﻿using InteControlSys.SampleService;
using InteControlSys.ViewModel;
using Microsoft.Win32;
using PropertyTools.Wpf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Linq;

namespace InteControlSys.ViewModel
{
    public class TrainArriveViewModel:NodeViewModel
    {
         public SampleService.SampleClient sp = new SampleClient("WSHttpBinding_ISample");
        public SampleCommon sc = new SampleCommon();

        public TrainArriveViewModel(string name)
        {
            this.Name = name;
            this.Parent = null;
            this.IsExpanded = true;
            this.NodeType = "TrainArriveViewModel";
            this.OperatingTruckList = new ObservableCollection<NodeViewModel>();

            Init();
         }

        public TrainArriveViewModel()
        {
            Init();
        }
        public override Uri GetMyIcon()
        {
            return new Uri("../Resources/Images/bucket.png", UriKind.RelativeOrAbsolute);
        }

        #region Properties
        private ObservableCollection<LongTrainList> longTrainList;
        public ObservableCollection<LongTrainList> LongTrainList
        {
            get { return longTrainList; }
            set
            {
                longTrainList = value;
                NotifyChanged("LongTrainList");
            }
        }

        private LongTrainList _selectedLongTrain;
        public LongTrainList SelectedLongTrain
        {
            get { return _selectedLongTrain; }
            set
            {
                _selectedLongTrain = value;
                NotifyChanged("SelectedLongTrain");
                GetPOrder();
            }
        }

        private ObservableCollection<POrder> pOrderList;
        public ObservableCollection<POrder> POrderList
        {
            get { return pOrderList; }
            set
            {
                pOrderList = value;
                NotifyChanged("POrderList");
            }
        }

        private POrder _selectedPOrder;
        public POrder SelectedPOrder
        {
            get { return _selectedPOrder; }
            set
            {
                _selectedPOrder = value;
                NotifyChanged("SelectedPOrder");
                GetList();
            }
        }

        private string _selectAllName;

        public string SelectAllName
        {
            get { return _selectAllName; }

            set
            {
                _selectAllName = value;
                NotifyChanged("SelectAllName");
            }
        }

        private bool _selectAll;

        public bool SelectAll
        {
            get { return _selectAll; }

            set
            {
                _selectAll = value;
                NotifyChanged("SelectAll");
                // MakeSelectAll();
            }
        }
        private ObservableCollection<TrainArrive2> trainArriveList;
        public ObservableCollection<TrainArrive2> TrainArriveList
        {
            get { return trainArriveList; }
            set
            {
                trainArriveList = value;
                NotifyChanged("TrainArriveList");
            }
        }

        private string _errorMSG = null;
        public string ErrorMSG
        {
            get { return _errorMSG; }
            set
            {
                _errorMSG = value;
                NotifyChanged("ErrorMSG");
            }
        }
       
        #endregion

        #region Command

        public void Init()
        {
            GetTree();
            _selectAll = false;
            _selectAllName = "全选";
        }
        public void GetTree()
        {
            var list = sp.GetArrivalBatch();
            longTrainList = new ObservableCollection<LongTrainList>();
            foreach (var item in list)
            {
                var a = new LongTrainList();
                a.ArrivalBatch = item;
                longTrainList.Add(a);
            }
            this.LongTrainList = longTrainList;
        }


        public void GetPOrder()
        {
            var list = sp.GetPOrderByArrival(SelectedLongTrain.ArrivalBatch);
            pOrderList = new ObservableCollection<POrder>();
            foreach (var item in list)
            {
                var po = new POrder();
                po.PID = item.ID;
                po.POrderID = item.PurchaseOrderID;
                po.StrMateriel = item.Materiel.Name;
                po.StrSupplier = item.Supplier.SupplierName;
                po.VechileCount = sp.VechileCount(SelectedLongTrain.ArrivalBatch, po.PID);
                pOrderList.Add(po);
            }
            this.POrderList = POrderList;
        }

        public void GetList()
        {
            var list = sp.GetTrain(SelectedLongTrain.ArrivalBatch,SelectedPOrder.PID);
            trainArriveList = new ObservableCollection<TrainArrive2>();
            foreach (var item in list)
            {
                var ta = new TrainArrive2();
                ta.IsSelected = false;
                ta.ID = item.ID;
                ta.POrderID = item.PurchaseOrder.PurchaseOrderID;
                ta.StrMateriel = item.PurchaseOrder.Materiel.Name;
                ta.StrSupplier = item.PurchaseOrder.Supplier.SupplierName;
                ta.StrCarrageNo = item.Conveyance.Identity;
                ta.StrStatus = sc.DicDiliverStatus[item.DeliverStatus];
                ta.ReceiveTime = item.ReceiveTime;
                //ta.ReceiveLocation = item.ReceiveLocation;
                trainArriveList.Add(ta);
            }
            this.TrainArriveList = trainArriveList;
        }

        public void MakeSelectAll()
        {
            if (trainArriveList != null && _selectAll == false)
            {
                foreach (var item in trainArriveList)
                {
                    item.IsSelected = true;
                }
                _selectAll = true;
                _selectAllName = "全不选";
            }
            else if (trainArriveList != null && _selectAll == true)
            {
                foreach (var item in trainArriveList)
                {
                    item.IsSelected = false;
                }
                _selectAll = false;
                _selectAllName = "全选";
            }
            this.TrainArriveList = trainArriveList;
            SelectAll = _selectAll;
            SelectAllName = _selectAllName;
        }

        public ICommand SelectOrUnSelectAll
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    MakeSelectAll();
                });
            }
        }

        public ICommand Query
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    GetTree();
                });
            }
        }

        public ICommand Recieve
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    int i = 0;
                    this.ErrorMSG = null;
                    trainArriveList = this.TrainArriveList;
                    foreach (var item in trainArriveList)
                    {
                        if (item.IsSelected)
                        {
                            //调用接车接口

                            if (sp.RecieveTrain(item.ID)) i++;

                        }
                    }
                    GetList();
                    if (i > 0) this.ErrorMSG = "已接车" + i.ToString() + "节";

                });
            }
        }

        public ICommand DropRecieve
        {
            get
            {
                return new DelegateCommand(() =>
                {
                     int i = 0;
                    this.ErrorMSG = null;
                    trainArriveList = this.TrainArriveList;
                    foreach (var item in trainArriveList)
                    {
                        if (item.IsSelected)
                        {
                            if (sp.DropRecieveTrain(item.ID)) i++;
                        }
                    }
                    GetList();
                    if (i > 0) this.ErrorMSG = "已取消接车" + i.ToString() + "节";

                
                });
            }
        }

        public ICommand New
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    GetTree();
                });
            }
        }

        //public ICommand Query
        //{
        //    get
        //    {
        //        return new DelegateCommand(() =>
        //        {
        //            GetTree();
        //        });
        //    }
        //}

        #endregion
    }

    public class LongTrainList
    {
        public LongTrainList() { }

        public string ArrivalBatch { get; set; }
    }

    public class POrder
    {
        public POrder() { }

        public Guid PID { get; set; }

        public string POrderID { get; set; }

        public string StrMateriel { get; set; }

        public Guid MaterielID { get; set; }
        public string StrSupplier { get; set; }

        public int VechileCount { get; set; }

        public int? InGroupCount { get; set; }
    }

    public class TrainArrive2 :INPCBase 
    {
        public TrainArrive2() { }

        private bool _isSelected = true;

        public bool IsSelected
        {
            get { return _isSelected; }

            set
            {
                _isSelected = value;
                NotifyChanged("IsSelected");
            }
        }

        private bool _notGrouped = true;
        public bool NotGrouped
        {
            get { return _notGrouped; }

            set
            {
                _notGrouped = value;
                NotifyChanged("NotGrouped");
            }
        }

        public Guid ID { get; set;
        }
        public string POrderID { get; set; }

        public string StrMateriel { get; set; }

        public string StrSupplier { get; set; }

        public string StrCarrageNo { get; set; }

        public string StrStatus { get; set; }

        public string ArrivalBatch { get; set; }

        public DateTime ReceiveTime { get; set; }

        public Guid MaterielID { get; set; }

        public Guid StockID { get; set; }
    }
}
